<?php

/*
 * Copyright (C) 2010 Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Description of MySQL
 *
 * @author Emanuele Colonnelli <emanuele.colonnelli@phpstruct.org>
 */

namespace org\phpstruct\db\driver;

use org\phpstruct\core;
use org\phpstruct\db\I;

/**
 * Description of DB
 *
 * @author emanuele.colonnelli
 */
class MySQL implements I\DBDriverInterface {

    private static $instance;
    private static $dbh;
    public static $typemap = array(
        'resource' => \PDO::PARAM_LOB,
        'integer' => \PDO::PARAM_INT,
        'string' => \PDO::PARAM_STR,
        'NULL' => \PDO::PARAM_NULL,
        'boolean' => \PDO::PARAM_BOOL
    );

//put your code here
    function __construct($dsn, $username, $password) {
//    $dsn = str_replace('mysql:dbname=','',$dsn);

//        self::$dbh = new \PDO($dsn, $username, $password, array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
        self::$dbh = new \PDO($dsn, $username, $password, array(\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
        self::$dbh->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        self::$dbh->setAttribute(\PDO::ATTR_CASE, \PDO::CASE_UPPER);


        if (!self::$dbh) {
            throw new \Exception("Connection error", -1, null);
        } else {
            self::$instance = $this;
        }
    }

//

    public function commit() {
        return self::$dbh->commit();
    }

//

    public function rollback() {
        return self::$dbh->rollback();
    }

//

    public function getAttribute($attr) {
        return self::$dbh->getAttribute($attr);
    }

    public function setAttribute($attr, $value) {
        return self::$dbh->setAttribute($attr, $value);
    }

    /**
     * @param <type> $sql
     * @return \org\phpstruct\db\PDO\PDOStatement
     */
    public function prepare($sql) {
//        return $stmt = new \org\phpstruct\db\PDO\PDOStatement(self::$dbh, self::$dbh->prepare($sql));
        return self::$dbh->prepare(strtoupper($sql));
    }

//

    public function exec($sql) {
        return self::$dbh->exec(strtoupper($sql));
    }

//

    /**
     *
     * @param <type> $sql
     * @return \PDOStatement
     */
    public function query($sql) {
        return self::$dbh->query(strtoupper($sql));
    }

//

    public function quote($sql) {
        return self::$dbh->quote($sql);
    }

//

    public function lastInsertId() {
        return self::$dbh->lastInsertId();
    }

    public function beginTransaction() {
        return self::$dbh->beginTransaction();
    }

}